﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFEntity;
using Iservices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using Newtonsoft.Json;
using Services;

namespace Hr.Controllers
{
    public class salaryGrantController : Controller
    {
        private readonly ILogger<clientController> _logger;
        private readonly MyDbContext myDbContext;
        private readonly Isalary_grantServices sgServices;
        private readonly Isalary_grant_detailsService sgdetail;
        private readonly Isalary_standard_detailsService ssdetail;

        public salaryGrantController(MyDbContext myDbContext, ILogger<clientController> logger, Isalary_grantServices sgServices, Isalary_grant_detailsService isg_DetailsService, Isalary_standard_detailsService iss_DetailsService)
        {
            this.myDbContext = myDbContext;
            _logger = logger;
            this.sgServices = sgServices;
            this.sgdetail = isg_DetailsService;
            this.ssdetail = iss_DetailsService;
        }


        public IActionResult register_locate()
        {
            return View();
        }


        [HttpGet]
        public IActionResult register_list(int kind)
        {
            ViewData["kind"] = kind;
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> register(int currentPage)
        {
            string where = " 1=1 and check_status=1 ";
            Dictionary<string, object> di = new Dictionary<string, object>();
            FenYe fy = new FenYe()
            {
                CurrentPage = currentPage,
                PageSize = 3,
                Where = where,
            };
            List<salary_grant> list = await sgServices.FenYeSel(fy);
            di["list"] = list;
            di["rows"] = fy.Rows;
            di["pages"] = fy.Pages;
            return Ok(di);
        }

        [HttpGet]
        public IActionResult register_commit(string sgid, string stid, string tsum, string psum, string humanNum, string kind)
        {

            ViewData["sgrantID"] = sgid;
            ViewData["standardID"] = stid;
            ViewData["standardSum"] = tsum;
            ViewData["paidSum"] = psum;
            ViewData["human_amount"] = humanNum;
            ViewData["kind"] = kind;
            return View();

        }

        [HttpPost]
        public async Task<IActionResult> register_commit(string sgrantID)
        {
            HttpContext.Session.Clear();
            List<salary_grant_details> list = await sgdetail.SelectAll();
            List<salary_grant_details> list2 = list.Where(e => e.salary_grant_id == sgrantID).ToList();
            Dictionary<string, object> di = new Dictionary<string, object>();
            di["leng"] = list2.Count;
            di["dt"] = list2;

            return Ok(di);
        }


        [HttpGet]
        //薪酬详情表
        public async Task<IActionResult> ssDetails(string standardID)
        {
            List<salary_standard_details> list = await ssdetail.SelectAll();
            List<salary_standard_details> list2 = list.Where(e => e.standard_id == standardID).ToList();
            Dictionary<string, object> di = new Dictionary<string, object>();
            string[] name = new string[list2.Count];
            double[] va = new double[list2.Count];
            for (int i = 0; i < list2.Count; i++)
            {
                name[i] = list2[i].standard_name;
                va[i] = list2[i].salary;
            }
            di["name"] = name;
            di["salary"] = va;
            return Content(JsonConvert.SerializeObject(di));
        }

        [HttpPost]
        public async Task<IActionResult> EditsgDetail(salary_grant_details detail)
        {
            int res = await sgdetail.Update(detail);
            HttpContext.Session.SetString("salary_grant_id", detail.salary_grant_id.ToString());
            HttpContext.Session.SetString("salary_paid_sum", detail.salary_paid_sum.ToString());
            HttpContext.Session.SetString("salary_standard_sum", detail.salary_standard_sum.ToString());
            return Content(JsonConvert.SerializeObject(res));
        }
        [HttpPost]
        public async Task<IActionResult> EditeGrant()
        {
            string grantID = HttpContext.Session.GetString("salary_grant_id");
            double psum = double.Parse(HttpContext.Session.GetString("salary_paid_sum"));
            double stsum = double.Parse(HttpContext.Session.GetString("salary_standard_sum"));
            int state = 2;
            List<salary_grant> list = await sgServices.SelectAll();
            salary_grant ss = list.Where(e => e.salary_grant_id == grantID).FirstOrDefault();
            ss.salary_paid_sum = psum;
            ss.salary_standard_sum = stsum;
            ss.check_status = state;
            string sql = string.Format(@"update dbo.salary_grant set salary_standard_sum='{0}',salary_paid_sum='{1}',check_status={2} where sgr_id={3}", ss.salary_standard_sum, ss.salary_paid_sum, ss.check_status, ss.sgr_id);
            int res = SqlHelper.ZSG(sql, "salaryGrantController");
            return Content(JsonConvert.SerializeObject(res));

        }

        public IActionResult register_success()
        {
            return View();
        }

        [HttpGet]
        public IActionResult check_list()
        {
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> check_list(int id)
        {
            string where = " 1=1 and check_status=2 ";
            Dictionary<string, object> di = new Dictionary<string, object>();
            FenYe fy = new FenYe()
            {

                CurrentPage = id,
                PageSize = 2,
                Where = where
            };
            List<salary_grant> list = await sgServices.FenYeSel(fy);
            di["list"] = list;
            di["rows"] = fy.Rows;
            di["pages"] = fy.Pages;
            return Ok(di);
        }

        [HttpGet]
        public IActionResult check(string sgid, string stid, string tsum, string psum, string humanNum)
        {
            ViewData["sgrantID"] = sgid;
            ViewData["standardID"] = stid;
            ViewData["standardSum"] = tsum;
            ViewData["paidSum"] = psum;
            ViewData["human_amount"] = humanNum;
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> check(string sgrantID)
        {
            List<salary_grant_details> list = await sgdetail.SelectAll();
            List<salary_grant_details> list2 = list.Where(e => e.salary_grant_id == sgrantID).ToList();
            Dictionary<string, object> di = new Dictionary<string, object>();
            di["leng"] = list2.Count;
            di["dt"] = list2;
            return Ok(di);
        }

        public async Task<IActionResult> ssDetails2(string standardID)
        {
            List<salary_standard_details> list = await ssdetail.SelectAll();
            List<salary_standard_details> list2 = list.Where(e => e.standard_id == standardID).ToList();
            return Ok(list2);
        }
        [HttpPost]
        public async Task<IActionResult> EditsgDetail2(salary_grant_details detail)
        {
            HttpContext.Session.Clear();
            int res = await sgdetail.Update(detail);
            HttpContext.Session.SetString("salary_grant_id", detail.salary_grant_id.ToString());
            HttpContext.Session.SetString("salary_paid_sum", detail.salary_paid_sum.ToString());
            return Content(JsonConvert.SerializeObject(res));
        }
        [HttpPost]
        public async Task<IActionResult> EditeGrant2()
        {
            string grantID = HttpContext.Session.GetString("salary_grant_id");
            double psum = double.Parse(HttpContext.Session.GetString("salary_paid_sum"));
            int state = 3;
            List<salary_grant> list = await sgServices.SelectAll();
            salary_grant ss = list.Where(e => e.salary_grant_id == grantID).FirstOrDefault();
            ss.salary_paid_sum = psum;
            ss.check_status = state;
            string sql = string.Format(@"update dbo.salary_grant set salary_standard_sum='{0}',salary_paid_sum='{1}',check_status={2} where sgr_id={3}", ss.salary_standard_sum, ss.salary_paid_sum, ss.check_status, ss.sgr_id);
            int res = SqlHelper.ZSG(sql, "salaryGrantController");
            return Content(JsonConvert.SerializeObject(res));
        }

        public IActionResult query_locate()
        {
            return View();
        }

        [HttpGet]
        public IActionResult query_list(string salaryID, string ke, string startTime, string endTime)
        {
            ViewData["salaryID"] = salaryID;
            ViewData["ke"] = ke;
            ViewData["startTime"] = startTime;
            ViewData["endTime"] = endTime;
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> query_list(int currentpage, string salaryID, string ke, string startTime, string endTime)
        {
            string where = " 1=1 and check_status=3 ";
            // where += $" and salary_grant_id like '%{salaryID}%' and first_kind_name like '%{ke}%' and regist_time  between '{startTime}' and '{endTime}' ";
            if (salaryID != "" && salaryID != null)
            {
                where += $" and salary_grant_id like '%{salaryID}%' ";
            }
            if (ke != "" && ke != null)
            {
                where += $"  and first_kind_name like '%{ke}%' ";
            }
            if (ke != "" && ke != null)
            {
                where += $"  and second_kind_name like '%{ke}%' ";
            }
            if (ke != "" && ke != null)
            {
                where += $"  and third_kind_name like '%{ke}%' ";
            }
            if (startTime != "" && startTime != null && endTime != "" && endTime != null)
            {
                where += $" and regist_time between '{startTime}' and '{endTime}'";
            }
            else if (startTime != null)
            {
                where += $"and [regist_time] >= '{startTime}' ";
            }
            else if (endTime != null)
            {
                where += $"and [regist_time] >= '{endTime}' ";
            }
            FenYe ye = new FenYe()
            {
                CurrentPage = currentpage,
                PageSize = 3,
                Where = where
            };
            Dictionary<string, object> di = new Dictionary<string, object>();
            List<salary_grant> list = await sgServices.FenYeSel(ye);
            di["list"] = list;
            di["rows"] = ye.Rows;
            di["pages"] = ye.Pages;
            return Ok(di);
        }

    }
}
